package tech.lq0.dreamaticvoyage.compat.jei;

import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.gui.builder.IRecipeLayoutBuilder;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.drawable.IDrawableAnimated;
import mezz.jei.api.gui.ingredient.IRecipeSlotsView;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.recipe.IFocusGroup;
import mezz.jei.api.recipe.RecipeIngredientRole;
import mezz.jei.api.recipe.RecipeType;
import mezz.jei.api.recipe.category.IRecipeCategory;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import tech.lq0.dreamaticvoyage.Utils;
import tech.lq0.dreamaticvoyage.block.entity.FukamizuFermenterBlockEntity;
import tech.lq0.dreamaticvoyage.init.ItemRegistry;
import tech.lq0.dreamaticvoyage.recipe.FukamizuFermentingRecipe;

import javax.annotation.ParametersAreNonnullByDefault;

public class FukamizuFermenterCategory implements IRecipeCategory<FukamizuFermentingRecipe> {

    public static final ResourceLocation TEXTURE = Utils.loc("textures/gui/fukamizu_fermenter.png");
    public static final RecipeType<FukamizuFermentingRecipe> TYPE = new RecipeType<>(Utils.loc("fukamizu_fermenter"),
            FukamizuFermentingRecipe.class);

    private final IDrawable background;
    private final IDrawable icon;
    private final IDrawableAnimated progress;

    public FukamizuFermenterCategory(IGuiHelper helper) {
        this.background = helper.drawableBuilder(TEXTURE, 34, 24, 115, 51)
                .setTextureSize(256, 256)
                .build();
        this.progress = helper.drawableBuilder(TEXTURE, 182, 0, 8, 25)
                .setTextureSize(256, 256)
                .buildAnimated(100, IDrawableAnimated.StartDirection.TOP, true);
        this.icon = helper.createDrawableIngredient(VanillaTypes.ITEM_STACK, new ItemStack(ItemRegistry.FUKAMIZU_FERMENTER.get()));
    }

    @Override
    @ParametersAreNonnullByDefault
    public void draw(FukamizuFermentingRecipe recipe, IRecipeSlotsView recipeSlotsView, GuiGraphics guiGraphics, double mouseX, double mouseY) {
        progress.draw(guiGraphics, 50, 16);
    }

    @Override
    public @NotNull RecipeType<FukamizuFermentingRecipe> getRecipeType() {
        return TYPE;
    }

    @Override
    public @NotNull Component getTitle() {
        return Component.translatable("gui.jei.category.dreamaticvoyage.fukamizu_fermenter");
    }

    @Override
    public @NotNull IDrawable getBackground() {
        return background;
    }

    @Override
    public @NotNull IDrawable getIcon() {
        return icon;
    }

    @Override
    @ParametersAreNonnullByDefault
    public void setRecipe(IRecipeLayoutBuilder builder, FukamizuFermentingRecipe recipe, IFocusGroup group) {
        var ingredients = recipe.getIngredients();

        builder.addSlot(RecipeIngredientRole.INPUT, 5, 8).addIngredients(ingredients.get(0));

        if (ingredients.size() >= 2) {
            builder.addSlot(RecipeIngredientRole.INPUT, 5, 29).addIngredients(ingredients.get(1));
        }

        if (!recipe.getInput().isEmpty()) {
            builder.addSlot(RecipeIngredientRole.INPUT, 29, 6)
                    .setFluidRenderer(FukamizuFermenterBlockEntity.MAX_FLUID_CAPACITY, true, 16, 42)
                    .addFluidStack(recipe.getInput().getFluid(), recipe.getInput().getAmount(), recipe.getInput().getTag());
        }

        builder.addSlot(RecipeIngredientRole.OUTPUT, 63, 6)
                .setFluidRenderer(FukamizuFermenterBlockEntity.MAX_FLUID_CAPACITY, true, 16, 42)
                .addFluidStack(recipe.getOutput().getFluid(), recipe.getOutput().getAmount(), recipe.getOutput().getTag());
    }
}
